﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Api.Filter;
using Customer.Bussiness_Service;
using Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ServiceCore;

namespace Customer.Areas.Card.Controllers
{
     
    [ApiController, Area("rk_card"), Route("/card/[action]")]
    public class card_pageController : ControllerBase
    {
        private readonly Icard_service _service;
        private readonly IToole_DI _tool;
        public card_pageController(Icard_service service, IToole_DI tool)
        {
            _service = service;
            _tool = tool;
        }
        /// <summary>
        /// 获取卡信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultObjModel get_card(string baseuserid, string card_no)
        {
            var result = _service.get_card(baseuserid, card_no);
            return result;
        }
     
    }  
    [ApiController, Area("rk_card"), Route("/card/[action]"), Customer_Admin]
    public class cardController : ControllerBase
    {
        private readonly Icard_service _service;
        private readonly IToole_DI _tool;
        public cardController(Icard_service service, IToole_DI tool)
        {
            _service = service;
            _tool = tool;
        }  
        /// <summary>
        /// 返回卡加密密码
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultObjModel encryption_card(string card_no)
        {
            var result = _service.encryption_card(card_no);
            return result;
        } 
        /// <summary>
        /// 卡录入
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel save_card(save_card_params Params)
        {
            var result = _service.save_card(Params);
            _tool.di_es.Add_Log($"==crm卡录入==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(Params)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");
            return result;
        }
        /// <summary>
        /// 编辑卡信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultObjModel edit_card(string card_id, string rk_baseuserid)//, string card_no, string card_pwd
        {
            var result = _service.edit_card(card_id, rk_baseuserid);// card_no, card_pwd,
            _tool.di_es.Add_Log($"==crm编辑卡信息==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { card_id, rk_baseuserid })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");//card_no, card_pwd,
            return result;
        }
        /// <summary>
        /// 获取卡列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel<get_card_pagelist_item> get_card_pagelist(get_card_pagelist_params Params)
        {
            var result = _service.get_card_pagelist(Params); 
            return result;
        }
        /// <summary>
        /// 删除/批量删除卡
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel del_card([FromBody]string[] card_ids)
        {
            var result = _service.del_card(card_ids);
            _tool.di_es.Add_Log($"==crm删除/批量删除卡==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(card_ids)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");
            return result;
        }
        /// <summary>
        /// 批量修改卡用户
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel batch_set_baseuserid(bacth_set_baseuserid_params Params)
        {
            var result = _service.batch_set_baseuserid(Params);
            _tool.di_es.Add_Log($"==crm批量修改卡用户==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(Params)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");
            return result;
        }
        /// <summary>
        /// 下载卡信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public void rk_card_down([FromForm]get_card_pagelist_params Params)
        {
            _service.rk_card_down(Params);
        }
        /// <summary>
        /// 批量修改卡状态
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel batch_change_state(batch_change_params Parmas)
        {
            var result = _service.batch_change_state(Parmas, true);
            _tool.di_es.Add_Log($"==crm批量修改卡状态==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(Parmas)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");
            return result;
        }
    } 
    [ApiController, Area("Customer"), Route("/card/rk/[action]"), RK_Admin]
    public class rk_cardController : ControllerBase
    {
        private readonly Icard_service _service;
        private readonly IToole_DI _tool;
        public rk_cardController(Icard_service service, IToole_DI tool)
        {
            _service = service;
            _tool = tool;
        }
        /// <summary>
        /// 返回卡加密密码
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultObjModel encryption_card(string card_no)
        {
            var result = _service.encryption_card(card_no);
            return result;
        }
        /// <summary>
        /// 卡录入
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel save_card_for_api(save_card_params Params)
        {
            Params.rk_baseuserid = _tool.di_user.RK_USER.BaseUserId;
            var result = _service.save_card(Params);
            _tool.di_es.Add_Log($"==商户{_tool.di_user.RK_USER.BaseUserId}卡录入==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(Params)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");
            return result;
        }
        /// <summary>
        /// 卡录入
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel save_card([FromForm]save_card_params Params)
        {
            Params.rk_baseuserid = _tool.di_user.RK_USER.BaseUserId;
            var result = _service.save_card(Params);
            _tool.di_es.Add_Log($"==商户{_tool.di_user.RK_USER.BaseUserId}卡录入==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(Params)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");
            return result;
        }
        /// <summary>
        /// 编辑卡信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultObjModel edit_card(string card_id)//, string card_no, string card_pwd
        {
            string rk_baseuserid = _tool.di_user.RK_USER.BaseUserId;
            var result = _service.edit_card(card_id, rk_baseuserid);// card_no, card_pwd,
            _tool.di_es.Add_Log($"==商户{_tool.di_user.RK_USER.BaseUserId}编辑卡信息==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { card_id, rk_baseuserid })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");//card_no, card_pwd,
            return result;
        }
        /// <summary>
        /// 获取卡列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel<get_card_pagelist_item> get_card_pagelist([FromForm]get_card_pagelist_params Params)
        {
            Params.rk_baseuserid = _tool.di_user.RK_USER.BaseUserId;
            var result = _service.get_card_pagelist(Params);
            return result;
        }
        /// <summary>
        /// 删除/批量删除卡
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel del_card([FromForm]string[] card_ids)
        {
            var result = _service.del_card(card_ids);
            _tool.di_es.Add_Log($"==商户{_tool.di_user.RK_USER.BaseUserId}删除/批量删除卡==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(card_ids)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");
            return result;
        }
        /// <summary>
        /// 批量修改卡用户
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel batch_set_baseuserid([FromForm]bacth_set_baseuserid_params Params)
        {
            Params.rk_baseuserid = _tool.di_user.RK_USER.BaseUserId;
            var result = _service.batch_set_baseuserid(Params);
            _tool.di_es.Add_Log($"==商户{_tool.di_user.RK_USER.BaseUserId}批量修改卡用户==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(Params)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");
            return result;
        }
        /// <summary>
        /// 下载卡信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public void rk_card_down([FromForm]get_card_pagelist_params Params)
        {
            Params.rk_baseuserid = _tool.di_user.RK_USER.BaseUserId;
            _service.rk_card_down(Params);
        } 
        /// <summary>
        ///  获取卡列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel<card_list_Item> get_card_list_for_shop([FromForm]card_search query)
        {
            var result = _service.get_card_list_for_shop(query);
            return result;
        }
        /// <summary>
        /// 充值管理 导出卡列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public void rk_card_down_for_shop([FromForm]card_search Params)
        { 
            _service.rk_card_down_for_shop(Params);
        }


        #region 卡充值等
        /// <summary>
        ///  开卡激活
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel start_activation_card([FromForm]activation_Params query)
        {
            var result = _service.start_activation_card(query);
            _tool.di_es.Add_Log($"==商户{_tool.di_user.RK_USER.BaseUserId}开卡激活==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(query)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");
            return result;
        } 
        /// <summary>
        ///  卡充值
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultObjModel start_recharge(string card_id, decimal? recharge_money)
        {
            var result = _service.start_recharge(card_id, recharge_money);
            _tool.di_es.Add_Log($"==商户{_tool.di_user.RK_USER.BaseUserId}卡充值==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { card_id, recharge_money })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");
            return result;
        }
        /// <summary>
        /// 保存卡信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel rk_edit_card([FromForm]edit_card_Params query)
        {
            var result = _service.edit_card(query);
            _tool.di_es.Add_Log($"==商户{_tool.di_user.RK_USER.BaseUserId}保存卡信息==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(query)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");
            return result;
        }
        /// <summary>
        /// 批量修改卡状态
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultObjModel batch_change_state([FromForm]batch_change_params Parmas)
        {
            var result = _service.batch_change_state(Parmas);
            _tool.di_es.Add_Log($"==商户{_tool.di_user.RK_USER.BaseUserId}批量修改卡状态==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(Parmas)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "card");
            return result;
        }
        /// <summary>
        /// 卡消费记录列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel<card_record_Item> card_record_pagelist([FromForm]card_record_search query)
        {
            var result = _service.card_record_pagelist(query);
            return result;
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public void rk_cardrecord_down_for_shop([FromForm]card_record_search query)
        {
            _service.rk_cardrecord_down_for_shop(query); 
        }
        #endregion
    }
}